home *** CD-ROM | disk | FTP | other *** search
/ ShareWare OnLine 2 / ShareWare OnLine Volume 2 (CMS Software)(1993).iso / elecmail / fido1000.zip / SRC1000.ZIP / GTMSG.C < prev    next >
C/C++ Source or Header  |  1993-05-21  |  17KB  |  731 lines

  1. /*  As some of the information contained herein is derived from information
  2.     gained through the distibution of source code by P&M Software, the
  3.     following notice is required:
  4.  
  5. ******************************************************************************
  6.  
  7.           Copyright (C)  P&M Software Co 1990.  All rights reserved
  8.  
  9.                   THIS PROGRAM BELONGS TO P&M SOFTWARE CO.
  10.  
  11.           The copyright owner hereby grants to you the right to
  12.       use this program as long as you abide by the following
  13.       restrictions:
  14.  
  15.           1.  Any product developed on information obtained herein
  16.           must be distributed free of charge.  In the same manner
  17.           this program was distributed.
  18.  
  19.           2.  Any product developed on information obtained herein
  20.           must be distributed in source code form.  In the same
  21.           manner this program was distributed.
  22.  
  23.           3.  This notice must not be removed.  And it must be included
  24.           in any product developed on information obtained herein.
  25.  
  26.  
  27. ******************************************************************************
  28. */
  29.  
  30.  
  31.  
  32.  
  33. /* code module for controlling the GT 16 message files */
  34.  
  35. /* #define DEBUGGING 1 */
  36.  
  37. #include    <bios.h>
  38. #include    <conio.h>
  39. #include    <dos.h>
  40. #include    <process.h>
  41. #include    <stdio.h>
  42. #include    <stdlib.h>
  43. #include    <string.h>
  44. #include    <time.h>
  45. #include    "datemath.h"
  46. #include    "gt16.h"
  47.  
  48.  
  49. /*─────────────────────────────────────────────────────────────────────────*/
  50.  
  51. int get_gt_lowest( char *msgpath )
  52. {
  53.    char        *fn;
  54.    FILE        *fp;
  55.    int         i;
  56.    MSG_RECORD  *temp_rec;
  57.  
  58.    fn = malloc(80);
  59.    temp_rec = malloc(sizeof(MSG_RECORD));
  60.  
  61.    if (fn == NULL) exit(1);
  62.    if (temp_rec == NULL) exit(1);
  63.  
  64.    strcpy(fn,msgpath);
  65.    strcat(fn,MSGCTL);
  66.  
  67.    if ((fp = fopen(fn,"r+b")) == NULL)
  68.    {
  69.       free(temp_rec);
  70.       free(fn);
  71.       printf("\nFailure to open: %s",fn);
  72.       return(-1);
  73.    }
  74.    if (fread(temp_rec,MSGCTLSZ,1,fp) == 0)
  75.    {
  76.       fclose(fp);
  77.       free(temp_rec);
  78.       free(fn);
  79.       printf("\nFailure reading: %s",fn);
  80.       return(-1);
  81.    }
  82.  
  83.    i = temp_rec->msg_number;
  84.  
  85.    fclose(fp);
  86.    free(temp_rec);
  87.    free(fn);
  88.  
  89.    return(i);
  90. }
  91.  
  92. /*─────────────────────────────────────────────────────────────────────────*/
  93.  
  94. int get_gt_highest ( char *msgpath )
  95. {
  96.    char         *fn;
  97.    FILE         *fp;
  98.    int          i;
  99.    MSG_RECORD   *temp_rec;
  100.  
  101.  
  102.    fn = malloc(80);
  103.    temp_rec = malloc(sizeof(MSG_RECORD));
  104.  
  105.    if (fn == NULL) exit(1);
  106.    if (temp_rec == NULL) exit(1);
  107.  
  108.    strcpy(fn,msgpath);
  109.    strcat(fn,MSGCTL);
  110.  
  111.    if ((fp = fopen(fn,"r+b")) == NULL)
  112.    {
  113.       free(temp_rec);
  114.       free(fn);
  115.       printf("\nFailed opening %s",fn);
  116.       return(-1);
  117.    }
  118.    if (fseek(fp,-128L,SEEK_END) == -1L)
  119.    {
  120.       fclose(fp);
  121.       free(temp_rec);
  122.       free(fn);
  123.       printf("\nFailed seeking %s",fn);
  124.       return(-1);
  125.    }
  126.    if (fread(temp_rec,MSGCTLSZ,1,fp) == 0)
  127.    {
  128.       fclose(fp);
  129.       free(temp_rec);
  130.       free(fn);
  131.       printf("\nFailed reading from %s",fn);
  132.       return(-1);
  133.    }
  134.  
  135.    i = temp_rec->msg_number;
  136.  
  137.    fclose(fp);
  138.    free(temp_rec);
  139.    free(fn);
  140.  
  141.    return(i);
  142. }
  143.  
  144. /*─────────────────────────────────────────────────────────────────────────*/
  145.  
  146. long get_gt_callernum( char *username )
  147. {
  148.     char        *fn,*gtpath;
  149.     FILE        *fp;
  150.     USER_RECORD *usr_rec;
  151.     long        caller_number;
  152.     int         loopgo,got,ret_val;
  153.  
  154.     usr_rec = malloc(300);
  155.     fn = malloc(80);
  156.  
  157.     if (usr_rec == NULL) exit(1);
  158.     if (fn == NULL) exit(1);
  159.  
  160.     gtpath = getenv("GTPATH");
  161.     strcpy(fn,gtpath);
  162.     strcat(fn,"\\");
  163.     strcat(fn,USERFILE);
  164.  
  165.     if ((fp = fopen(fn,"r+b")) == NULL)
  166.     {
  167.         free(fn);
  168.         free(usr_rec);
  169.         printf("\nON FIRST OPENING - %s\n",fn);
  170.         return(-1L);
  171.     }
  172.  
  173.     caller_number = (long) 0;
  174.     while(loopgo)
  175.     {
  176.        got = fread(usr_rec,sizeof(USER_RECORD),1,fp);
  177.        if (got==0)
  178.        {
  179.            printf("\nReached end of fn: %s",fn);
  180.            loopgo = 0;
  181.        }
  182.        else
  183.        {
  184.            ret_val = stricmp(usr_rec->user_name,username);
  185.            if (ret_val == 0)
  186.            {
  187.               loopgo = 0;
  188.               caller_number = usr_rec->user_caller_no;
  189.            }
  190.        }
  191.     }
  192.  
  193.     fclose(fp);
  194.  
  195.     if (caller_number == 0)
  196.     {
  197.        free(fn);
  198.        free(usr_rec);
  199.        return(-1L);
  200.     }
  201.  
  202.     free(fn);
  203.     free(usr_rec);
  204.     return(caller_number);
  205.  
  206.     /* end of getting caller number section. */
  207. }
  208.  
  209. /*─────────────────────────────────────────────────────────────────────────*/
  210.  
  211. int get_gt_lastread(long usernum, char *mpath )
  212. /* usernum is caller number */
  213. /* mpath is message area path */
  214.  
  215. {
  216.     int                 loopgo,last_processed,got, low;
  217.     char                *fn;
  218.     USER_MSG_RECORD     *usr_msg;
  219.     FILE                *fp;
  220.  
  221.     low = get_gt_lowest(mpath);
  222.  
  223.     fn = malloc(80);
  224.     usr_msg = malloc(300);
  225.  
  226.     if (fn == NULL) exit(1);
  227.     if (usr_msg == NULL) exit(1);
  228.  
  229.     strcpy(fn,mpath);
  230.     strcat(fn,USRMSG);
  231.  
  232.     if ((fp = fopen(fn,"r+b")) == NULL)
  233.     {
  234.         free(usr_msg);
  235.         free(fn);
  236.         printf("\nFailed to open fn: %s",fn);
  237.         return(-1);
  238.     }
  239.  
  240.     last_processed = -1L;
  241.     loopgo = 1;
  242.  
  243.     while (loopgo)
  244.     {
  245.         got = fread(usr_msg,sizeof(USER_MSG_RECORD),1,fp);
  246.         if (got==0)
  247.         {
  248.             printf("\nReached end of file: %s",fn);
  249.             loopgo = 0;
  250.         }
  251.         else
  252.         {
  253.             if (usr_msg->user_msg_caller_no == usernum)
  254.             {
  255.                last_processed = usr_msg->user_msg_prior_read;
  256.                if (last_processed < low) last_processed = low;
  257.                loopgo = 0;
  258.             }
  259.         }
  260.      }
  261.      fclose(fp);
  262.  
  263.      free(usr_msg);
  264.      free(fn);
  265.      return(last_processed);
  266. }
  267.  
  268. /*─────────────────────────────────────────────────────────────────────────*/
  269.  
  270. int get_gt_message(int msgnumber,        char *msgpath,
  271.                    MSG_RECORD *msg_rec,  char *mbuffer)
  272. {
  273.    int          lowest, highest, i, som_offset;
  274.    long         offset;
  275.  
  276.    char         *fn, *tempstr;
  277.    char         som_start[10],som_end[10];
  278.    FILE         *fp;
  279.  
  280.    fn = malloc(80);
  281.    tempstr = malloc(MAXGTLLEN + 1);
  282.  
  283.    lowest = get_gt_lowest(msgpath);
  284.    highest = get_gt_highest(msgpath);
  285.  
  286.    if ((msgnumber < lowest) || (msgnumber > highest))
  287.    {
  288.       free(tempstr);
  289.       free(fn);
  290.       return(-1);
  291.    }
  292.  
  293.    offset = (long) (msgnumber - lowest);
  294.    offset = (offset * (long) MSGCTLSZ);
  295.  
  296.    strcpy(fn,msgpath);
  297.    strcat(fn,MSGCTL);
  298.    if ((fp = fopen(fn,"r+b")) == NULL)
  299.    {
  300.       free(tempstr);
  301.       free(fn);
  302.       return(-1);
  303.    }
  304.    if (fseek(fp,offset,SEEK_SET) == -1L)
  305.    {
  306.       fclose(fp);
  307.       free(tempstr);
  308.       free(fn);
  309.       return(-1);
  310.    }
  311.    if (fread(msg_rec,MSGCTLSZ,1,fp) == 0)
  312.    {
  313.       fclose(fp);
  314.       free(tempstr);
  315.       free(fn);
  316.       return(-1);
  317.    }
  318.    fclose(fp);
  319.  
  320. #ifdef DEBUGGING
  321.    prn_gt_msg_hdr((void *) msg_rec);
  322. #endif
  323.  
  324.    strcpy(fn,msgpath);
  325.    strcat(fn,FILEPATH);
  326.  
  327.    i = ((msgnumber - 1) / 10) + 1;
  328.    sprintf(fn,"%s%s%05d.MES",msgpath,FILEPATH,i );
  329.  
  330. #ifdef DEBUGGING
  331.    printf("\nReading from: %s\n",fn);
  332. #endif
  333.  
  334.    if ((fp = fopen(fn,"r")) == NULL)
  335.    {
  336.       free(tempstr);
  337.       free(fn);
  338.       return(-1);
  339.    }
  340.  
  341.    i = (msgnumber - 1) % 10;
  342.    som_offset = i;
  343.    sprintf(som_start,"%c%s%d",(char) 24,"SOM",i);
  344.    i++;
  345.    sprintf(som_end,"%c%s",(char) 24,"SOM");
  346.    strcpy(tempstr," ");
  347.    i = 0;
  348.  
  349.  
  350. #ifdef DEBUGGING
  351.    printf("\nSearching for SOM start: %s",som_start);
  352. #endif
  353.  
  354.    while (strncmp(tempstr,som_start,5))
  355.    {
  356.       fgets(tempstr,MAXGTLLEN,fp);
  357.       if (ferror(fp))
  358.       {
  359.          fclose(fp);
  360.          free(tempstr);
  361.          free(fn);
  362.          return(-1);
  363.       }
  364.       if (feof(fp))
  365.       {
  366.          fclose(fp);
  367.          free(tempstr);
  368.          free(fn);
  369.          return(-1);
  370.       }
  371.    }
  372.  
  373.    strcpy(mbuffer,"");
  374.  
  375.    fgets(tempstr,MAXGTLLEN,fp);
  376.    while ((strncmp(tempstr,som_end,4)) && !(feof(fp)))
  377.    {
  378.       strcat(mbuffer,tempstr);
  379.       fgets(tempstr,MAXGTLLEN,fp);
  380.    }
  381.  
  382. #ifdef DEBUGGING
  383.    puts("\nMessage Text follows: \n");
  384.    puts(mbuffer);
  385. #endif
  386.  
  387.    fclose(fp);
  388.    free(tempstr);
  389.    free(fn);
  390.  
  391.    return(0);
  392. }
  393.  
  394. /*─────────────────────────────────────────────────────────────────────────*/
  395.  
  396. /* should return either the message number written, or zero if failure */
  397.  
  398. int append_gt_message(char *msgpath, MSG_RECORD *msg_rec, char *mbuffer)
  399. {
  400.    int     highest, som_offset, nextmsg, i;
  401.    char    *fn, *tempstr;
  402.    char    som_next[10];
  403.    FILE    *fp;
  404.  
  405.    fn = malloc(80);
  406.    tempstr = malloc(100);
  407.  
  408.    highest = get_gt_highest(msgpath);
  409.    nextmsg = highest + 1;
  410.    msg_rec->msg_number = nextmsg;
  411.  
  412.    strcpy(fn,msgpath);
  413.    strcat(fn,MSGCTL);
  414.    if ((fp = fopen(fn,"ab")) == NULL)
  415.    {
  416.       free(tempstr);
  417.       free(fn);
  418.       return(-1);
  419.    }
  420.  
  421.    if (fwrite(msg_rec,MSGCTLSZ,1,fp) == 0)
  422.    {
  423.       fclose(fp);
  424.       free(tempstr);
  425.       free(fn);
  426.       return(-1);
  427.    }
  428.    fclose(fp);
  429.  
  430.    som_offset = (nextmsg - 1) % 10;
  431.    sprintf(som_next,"%c%s%d",(char) 24,"SOM",som_offset);
  432.  
  433.    strcpy(fn,msgpath);
  434.    strcat(fn,FILEPATH);
  435.    i = ((nextmsg - 1) / 10) + 1;
  436.    sprintf(fn,"%s%s%05d.MES",msgpath,FILEPATH,i );
  437.  
  438.    if ((fp = fopen(fn,"at")) == NULL)
  439.    {
  440.       free(tempstr);
  441.       free(fn);
  442.       return(-1);
  443.    }
  444.    fputs(som_next,fp);
  445.    fputs("\n",fp);
  446.    fputs(mbuffer,fp);
  447.    fputs("\n",fp);
  448.    fclose(fp);
  449.  
  450.    free(tempstr);
  451.    free(fn);
  452.    return(nextmsg);
  453.  
  454. }
  455.  
  456. /*─────────────────────────────────────────────────────────────────────────*/
  457.  
  458. int prn_gt_msg_hdr(MSG_RECORD *msg_rec)
  459. {
  460.    printf("\n───────────────────────────────────────────────────────────────");
  461.    printf("\nMessage number: %d",msg_rec->msg_number);
  462.    printf("\nFrom: %s   To: %s",msg_rec->msg_sender,msg_rec->msg_addressee);
  463.    printf("\nAbout: %s   Entered on: %s",msg_rec->msg_topic,msg_rec->msg_date);
  464.    printf("\nFlags  Sticky: %d   Fidoorg: %d ",msg_rec->msg_flags.sticky,msg_rec->msg_flags.fidoorg);
  465.    printf("\n───────────────────────────────────────────────────────────────");
  466.    printf("\n\n");
  467.    return(0);
  468. }
  469.  
  470. /*──────────────────────────────────────────────────────────────────────────*/
  471.  
  472. int set_gt_lastread(long callernum, char *mpath, int pnter)
  473. {
  474.     int                 loopgo,got,found,i,j;
  475.     char                *fn;
  476.     USER_MSG_RECORD     *uma[MAX_USR_MSG];
  477.     FILE                *fp;
  478.  
  479.     fn = malloc(80);
  480.     if (fn == NULL)
  481.     {
  482.         printf("\nMemory allocation error on fn");
  483.         exit(1);
  484.     }
  485.  
  486.     for (j=0;j<MAX_USR_MSG;j++)
  487.     {
  488.         uma[j]=malloc(sizeof(USER_MSG_RECORD));
  489.  
  490.         if (uma[j]==NULL)
  491.         {
  492.             printf("\nMemory allocation error at position: %d",j);
  493.             exit(1);
  494.         }
  495.     }
  496.  
  497.     strcpy(fn,mpath);
  498.     strcat(fn,USRMSG);
  499.  
  500.     if ((fp = fopen(fn,"r+b")) == NULL)
  501.     {
  502.         for (j=(MAX_USR_MSG - 1);j>=0;j--) free(uma[j]);
  503.         free(fn);
  504.         return(-1);
  505.     }
  506.  
  507.     loopgo = 1;  found = -1;  i = 0;
  508.  
  509.     while (loopgo)
  510.     {
  511.        got = fread(uma[i],sizeof(USER_MSG_RECORD),1,fp);
  512.  
  513.        if (got==0)
  514.        {
  515.            loopgo = 0;
  516.        }
  517.        else
  518.        {
  519.            if (uma[i]->user_msg_caller_no == callernum)
  520.            {
  521.               found = i;
  522.               uma[i]->user_msg_prior_read = pnter;
  523.            }
  524.            i++;
  525.        }
  526.     }
  527.     fclose(fp);
  528.  
  529.     if (found > -1)
  530.     {
  531.         if ((fp = fopen(fn,"wb")) == NULL)
  532.         {
  533.             for (j=(MAX_USR_MSG - 1);j>=0;j--) free(uma[j]);
  534.             free(fn);
  535.             return(-1);
  536.         }
  537.  
  538.         for (j=0;j<i;j++)
  539.         {
  540.             fwrite(uma[j],sizeof(USER_MSG_RECORD),1,fp);
  541.         }
  542.         fclose(fp);
  543.     }
  544.     for (j=(MAX_USR_MSG - 1);j>=0;j--) free(uma[j]);
  545.     free(fn);
  546.     return(0);
  547. }
  548.  
  549. /* ──────────────────────────────────────────────────────────────────── */
  550.  
  551. #define MAXBUFSZ 30000
  552. int filecopy(char *source, char *dest)
  553. {
  554.  
  555.     FILE    *fin,*fout;
  556.     char    *buffer;
  557.     int     datalen,written;
  558.  
  559.     buffer = malloc(MAXBUFSZ);
  560.  
  561.     if ((fin = fopen(source,"rb")) == NULL)
  562.     {
  563.         free(buffer);
  564.         return(-1);
  565.     }
  566.  
  567.     if ((fout = fopen(dest,"wb")) == NULL)
  568.     {
  569.         free(buffer);
  570.         return(-1);
  571.     }
  572.     while (!(feof(fin)))
  573.     {
  574.         datalen = fread(buffer,1,MAXBUFSZ,fin);
  575.         if (datalen) written = fwrite(buffer,1,datalen,fout);
  576.         if (datalen != written)
  577.         {
  578.             printf("\nOoops...GTMSG.C lib API failure\n");
  579.             exit(1);
  580.         }
  581.     }
  582.     fclose(fout);
  583.     fclose(fin);
  584.     free(buffer);
  585.     return(0);
  586.  
  587. }
  588.  
  589. /* ──────────────────────────────────────────────────────────────────── */
  590.  
  591. int get_next_nf(char *mpath, int stnum)
  592. {
  593.     char        *fn;
  594.     MSG_RECORD  *wk_rec;
  595.     int         i,j;
  596.     FILE        *msgctl;
  597.     long        offset;
  598.     int         highest,lowest,startnum;
  599.  
  600.     startnum = stnum;   /* don't particularly like the possibility of modifying
  601.                            a function parameter value, even though it probably
  602.                            would work file...   */
  603.  
  604.     highest = get_gt_highest(mpath);
  605.     lowest = get_gt_lowest(mpath);
  606.  
  607.     if (startnum >= highest) return(highest + 1);
  608.     if (startnum < lowest) startnum = lowest;
  609.  
  610.     fn = malloc(80);
  611.     wk_rec = malloc(MSGCTLSZ+1);
  612.  
  613.     if (fn == NULL) exit(1);
  614.     if (wk_rec == NULL) exit(1);
  615.  
  616.     strcpy(fn,mpath);
  617.     strcat(fn,MSGCTL);
  618.     if ((msgctl = fopen(fn,"rb")) == NULL)
  619.     {
  620.         printf("\nFailed to open: %s",fn);
  621.         free(wk_rec);
  622.         free(fn);
  623.         return(-1);
  624.     }
  625.     if (startnum > lowest)   /* seek to starting location */
  626.     {
  627.         offset = (long) (startnum - lowest);
  628.         offset = (offset * (long) MSGCTLSZ);
  629.         if (fseek(msgctl,offset,SEEK_SET) == -1L)
  630.         {
  631.            fclose(msgctl);
  632.            free(wk_rec);
  633.            free(fn);
  634.            return(-1);
  635.         }
  636.     }
  637.  
  638.     i = highest;
  639.  
  640.     while (!(feof(msgctl)))
  641.     {
  642.         j = fread(wk_rec,MSGCTLSZ,1,msgctl);
  643.         if (j==0) break;
  644.         if ( (wk_rec->msg_number > startnum) && (wk_rec->msg_flags.fidoorg == 0) )
  645.         {
  646.             i = wk_rec->msg_number;
  647.             fclose(msgctl);
  648.             free(wk_rec);
  649.             free(fn);
  650.             return(i);
  651.         }
  652.     }
  653.     i++;
  654.     fclose(msgctl);
  655.     free(wk_rec);
  656.     free(fn);
  657.     return(i);
  658. }
  659.  
  660. /* ──────────────────────────────────────────────────────────────────── */
  661.  
  662. char *get_gt_nmaildir(void)
  663. {
  664.     char    *gtpath;
  665.     char    *fn;
  666.     char    *workstr;
  667.     FILE    *fpcnf;
  668.     char    *nmail;
  669.     char    *access_lvl, *direntry;
  670.  
  671.     nmail = malloc(80);    /* this STAYS!!! */
  672.     fn = malloc(100);
  673.     workstr = malloc(110);
  674.     access_lvl = malloc(100);
  675.     direntry = malloc(100);
  676.  
  677.     gtpath = getenv("GTPATH");
  678.     strcpy(fn,gtpath);
  679.     strcat(fn,"\\gtmdir.bbs");
  680.     if ((fpcnf = fopen(fn,"rt")) == NULL)
  681.     {
  682.         printf("\nError opening fn: %s\n",fn);
  683.         free(direntry);
  684.         free(access_lvl);
  685.         free(workstr);
  686.         free(fn);
  687.         free(nmail);
  688.         return(NULL);
  689.     }
  690.  
  691.     memset(workstr,0,20);  /* don't forget to initialize variables you are
  692.                               going to test against blindly */
  693.  
  694.     while (1)
  695.     {
  696.         memset(workstr,0,20);
  697.  
  698.         if (fgets(workstr,110,fpcnf) == NULL)
  699.         {
  700.             free(direntry);
  701.             free(access_lvl);
  702.             free(workstr);
  703.             free(fn);
  704.             free(nmail);
  705.             return(NULL);
  706.         }
  707.  
  708.         sscanf(workstr,"%s %s",access_lvl,direntry);
  709.  
  710.         if (strstr(direntry,"~"))
  711.         {
  712.             strcpy(nmail, (strstr(direntry,":") - 1) );
  713.             strcat(nmail,"\\");
  714.             fclose(fpcnf);
  715.             free(direntry);
  716.             free(access_lvl);
  717.             free(workstr);
  718.             free(fn);
  719.             return(nmail);
  720.         }
  721.     } /* should NOT get past here, but.... */
  722.     fclose(fpcnf);
  723.     free(direntry);
  724.     free(access_lvl);
  725.     free(workstr);
  726.     free(fn);
  727.     free(nmail);
  728.     return(NULL);
  729. }
  730.  
  731.